package com.note.servlet.type;

import com.alibaba.fastjson.JSON;
import com.note.service.note.noteService;
import com.note.service.note.noteServiceImpl;
import com.note.utils.BaseCC;
import lombok.SneakyThrows;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.Map;

public class addType extends HttpServlet {
    @SneakyThrows
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String typeName = req.getParameter("typeName");
        noteService n = new noteServiceImpl();
        boolean b = n.addType(typeName);
        Map<String, Object> map = new HashMap<String, Object>();
        Connection conn = BaseCC.getConnection();
        String sql = "select * from notetype where typename=?";
        PreparedStatement ps = null;
        ResultSet rs = null;
        Object[] params = {typeName};
        rs = BaseCC.execute(conn, sql, params, ps, rs);
        int id=-1;
        if(rs.next()){
            id = rs.getInt("typeid");
        }
        BaseCC.closeResource(conn,ps,rs);
        map.put("typeid", String.valueOf(id));
        map.put("flag", String.valueOf(b));
        String mapJson = JSON.toJSONString(map);
        PrintWriter out = resp.getWriter();
        out.write(mapJson);
        out.close();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
